package com.tebiecloud.store.server.controller.feign;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.store.client.model.entity.Store;
import com.tebiecloud.store.client.model.entity.StoreStaff;
import com.tebiecloud.store.client.model.vo.StoreVO;
import com.tebiecloud.store.client.service.IStoreServiceClient;
import com.tebiecloud.store.server.service.StoreService;
import com.tebiecloud.store.server.service.StoreStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

import static org.assertj.core.util.Arrays.asList;


@RestController
@RequestMapping("/feign/store")
public class StoreStoreController implements IStoreServiceClient {

    @Autowired
    StoreService storeService;

    @Autowired
    StoreStaffService storeStaffService;

    @Autowired
    IStoreServiceClient iStoreServiceClient;

    @PostMapping("getstore")
    @Override
    public ResultBody<Store> getStore(@RequestParam("storeId") long storeId) {
        QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
        storeQueryWrapper.in("store_id", storeId);
        storeQueryWrapper.eq("status", 1);
        storeQueryWrapper.eq("deleted", 0);
        Store store = storeService.getOne(storeQueryWrapper);
        return ResultBodyUtils.ok(store);
    }

    /**
     * 获取门店列表
     *
     * @param storeIds 门店id，多个用逗号隔开
     * @return
     */
    @PostMapping("selectstoreList")
    @Override
    public ResultBody<List<Store>> selectStoreList(@RequestParam("storeIds") String storeIds) {
        List<Store> storeList = new ArrayList<>();
        QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
        storeQueryWrapper.in("store_id", storeIds);
        storeQueryWrapper.eq("status", 1);
        storeQueryWrapper.eq("deleted", 0);
        storeList = storeService.list(storeQueryWrapper);
        return ResultBodyUtils.ok(storeList);
    }

    /**
     * 查询所有门店
     *
     * @return
     */
    @PostMapping("list")
    @Override
    public ResultBody<List<Store>> list() {
        //往后增加逻辑，目前第一家
        QueryWrapper<Store> storeQueryWrapper = new QueryWrapper<>();
        storeQueryWrapper.orderByDesc("sort");
        List<Store> storeList = storeService.list(storeQueryWrapper);
        return ResultBodyUtils.ok(storeList);
    }

    /**
     * @return
     */
    @PostMapping("listBynearby")
    @Override
    public ResultBody<List<StoreVO>> listBynearby(@RequestParam(value = "lng", required = false) Double lng,
                                                  @RequestParam(value = "lat", required = false) Double lat,
                                                  @RequestParam(value = "storeIds", required = false) String storeIds) {
        List<String> storeIdList = null;
        if (StringUtils.isNotEmpty(storeIds)) {
            String[] strings = storeIds.split(",");
            storeIdList = new ArrayList<>();
            for (int i = 0; i < strings.length; i++) {
                storeIdList.add(strings[i]);
            }
        }
        List<StoreVO> storeList = storeService.listNearby(lng, lat, storeIdList);
        return ResultBodyUtils.ok(storeList);
    }

    @PostMapping("updateStoreBalance")
    @Override
    public ResultBody<Store> updateStoreBalance(long storeId, int balance) {
        Store store = storeService.updateBalance(storeId, balance);
        if (store == null) {
            return ResultBodyUtils.failed();
        }
        return ResultBodyUtils.ok(store);
    }

    /**
     * 添加管辖店铺
     *
     * @param name
     * @return
     */
    @PostMapping("select")
    public ResultBody select(@RequestParam("name") String name,
                             @RequestParam("username") String username,
                             @RequestParam("mobile") String mobile,
                             @RequestParam("sex") Integer sex,
                             @RequestParam("userId")Long userId) {

        String[] split = name.split(",");
        if (split.length>1){
            for (String string : split) {
                QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("name", string);
                Store one = storeService.getOne(queryWrapper);
                if (one == null) return ResultBodyUtils.failed();
                StoreStaff storeStaff=new StoreStaff();
                storeStaff.setName(username);
                storeStaff.setUserId(userId);
                storeStaff.setPhone(mobile);
                storeStaff.setRoleCode("AGM");
                storeStaff.setStoreId(one.getStoreId());
                storeStaff.setEntryTime(new Date());
                storeStaff.setSex(sex);
                storeStaff.setCt(new Date());
                storeStaff.setUt(new Date());
                storeStaffService.save(storeStaff);
            }
        }
        QueryWrapper<Store> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        Store one = storeService.getOne(queryWrapper);
        if (one == null) return ResultBodyUtils.failed();
        StoreStaff storeStaff=new StoreStaff();
        storeStaff.setName(username);
        storeStaff.setUserId(userId);
        storeStaff.setPhone(mobile);
        storeStaff.setRoleCode("AGM");
        storeStaff.setStoreId(one.getStoreId());
        storeStaff.setEntryTime(new Date());
        storeStaff.setSex(sex);
        storeStaff.setCt(new Date());
        storeStaff.setUt(new Date());
        storeStaffService.save(storeStaff);

        return ResultBodyUtils.ok();
    }

    /**
     * agm管辖店铺数量
     *
     * @param userId
     * @return
     */
    @Override
    @PostMapping("count")
    public Integer count(@RequestParam("userId") Long userId) {
        QueryWrapper<StoreStaff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("role_code", "AGM");
        int count = storeStaffService.count(queryWrapper);
        return count;
    }

    /**
     * agm店铺详细信息查询
     *
     * @param userId
     * @return
     */
    @Override
    @PostMapping("agmxx")
    public List<Store> agmxx(Long userId) {
       List<Store>lists=new ArrayList<>();
        QueryWrapper<StoreStaff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<StoreStaff> list1 = storeStaffService.list(queryWrapper);
        for (StoreStaff storeStaff : list1) {
            QueryWrapper<Store> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("store_id", storeStaff.getStoreId());
            List<Store> list = storeService.list(queryWrapper1);
            for (Store store : list) {
                lists.add(store);
            }
        }

        return lists;
    }

}
